package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.SchoolbagPhaseDtoMapper;
import com.starlink.model.domain.SchoolbagPhaseDO;
import com.starlink.mapper.SchoolbagPhaseMapper;
import com.starlink.model.request.SchoolbagPhaseRequest;
import com.starlink.service.SchoolbagPhaseService;
import com.starlink.model.util.SchoolbagPhaseConvert;
import com.starlink.model.vo.SchoolbagPhaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagPhaseServiceImpl extends ServiceImpl<SchoolbagPhaseMapper, SchoolbagPhaseDO> implements SchoolbagPhaseService {
    @Autowired
    private SchoolbagPhaseMapper schoolbagPhaseMapper;

    @Override
    public SchoolbagPhaseVO getByKey(long id) {
        SchoolbagPhaseVO schoolbagPhaseVO = SchoolbagPhaseDtoMapper.INSTANCE.do2VO(getById(id));
        return schoolbagPhaseVO;
    }

    @Override
    public PageInfo<SchoolbagPhaseVO> pageQuery(Pageable pageable,SchoolbagPhaseRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagPhaseDO> list = schoolbagPhaseMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<SchoolbagPhaseVO> schoolbagPhaseVOList = SchoolbagPhaseConvert.buildSchoolbagPhaseVoList(list);
        return new PageInfo(schoolbagPhaseVOList);
    }

    @Override
    public List<SchoolbagPhaseVO> listQuery(SchoolbagPhaseRequest request) {
        List<SchoolbagPhaseDO> list = schoolbagPhaseMapper.selectDOList(SchoolbagPhaseDtoMapper.INSTANCE.request2DO(request));
        List<SchoolbagPhaseVO> schoolbagPhaseVOS = SchoolbagPhaseConvert.buildSchoolbagPhaseVoList(list);
        return schoolbagPhaseVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagPhaseRequest request) {
        SchoolbagPhaseDO schoolbagPhaseDO = SchoolbagPhaseDtoMapper.INSTANCE.request2DO(request);
        save(schoolbagPhaseDO);
        request.setId(schoolbagPhaseDO.getId());
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagPhaseRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagPhaseDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        schoolbagPhaseMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
